package view.components
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import utils.animate.TweenLight;
	
	public class AnimateShow extends Sprite
	{
		public static const DIRECTION_TOP:int = 0;
		public static const DIRECTION_BOTTOM:int = 1;
		public static const DIRECTION_LEFT:int = 2;
		public static const DIRECTION_RIGHT:int = 3;
		
		private var rect:Rectangle; 
		private var target:DisplayObject;
		private var tween:TweenLight;
		private var direction:int;
		
		public function AnimateShow()
		{
			initRect();	
			initTweenLight();
		}
		
		private function initTweenLight():void
		{
			tween = new TweenLight();
		}
		
		private function initRect():void
		{
			rect = new Rectangle();
		}
		
		public function set duration(v:Number):void
		{
			tween.duration = v;
		}
		
		public function setTarget(target:DisplayObject):void
		{
			this.target = target;
			rect.width = target.width;
			rect.height = target.height;
			
			var fxy:Point = getFromXY();
			
			target.x = fxy.x;
			target.y = fxy.y;
			
			addChild(target);
			this.scrollRect = rect
		}
		
		public function setDirection(direction:int):void
		{
			this.direction = direction;
		}
		
		public function start():void
		{
			var fxy:Point = getFromXY();
			tween.stop();
			tween.setTarget(target,fxy.x,fxy.y,0,0);
			tween.start();
			
		}
		
		public function back():void
		{
			tween.stop();
			var fxy:Point = getFromXY();
			tween.setTarget(target,target.x,target.y,fxy.x,fxy.y);
			tween.start();
		}
		
		private function getFromXY():Point
		{
			var p:Point = new Point();
			switch(direction)
			{
				case DIRECTION_TOP:
					p.y = -target.height;
					break;
				case DIRECTION_BOTTOM:
					p.y = 2 * target.height;
					break;
				case DIRECTION_LEFT:
					p.x = - target.width;
					break;
				case DIRECTION_RIGHT:
					p.y = 2 * target.width;
					break;
			}
			return p;
		}
	}
}